/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  TraceFilterRuleHelper.java                                      
 *  File Contents Outline :  Trace过滤规则持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.dbhelper;


import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbTraceFilterRule;
import storage.util.StorageManager;

public class TraceFilterRuleHelper {

    public void saveTraceFilterRule(DbTraceFilterRule traceFilterRule) {
       PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_traceFilterRule"));
//        sm.open();
        sm.setRowKey(traceFilterRule.getRowKey());
        sm.put(DbTraceFilterRule.cl_name, traceFilterRule.getName());
        sm.put(DbTraceFilterRule.cl_traceFilterRule, traceFilterRule.getTraceFilterRule());
        sm.put(DbTraceFilterRule.cl_filterType, traceFilterRule.getFilterType());
        sm.put(DbTraceFilterRule.cl_createTime, traceFilterRule.getCreateTime());
//        sm.close();
    }

    /**
     *
     * @param columnName
     * @param value
     * @return
     */
    public List<DbTraceFilterRule> getTraceFilterRules(final String columnName, final String value) {
        List<DbTraceFilterRule> result = new LinkedList<DbTraceFilterRule>();
         PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_traceFilterRule"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String name = null, traceFilterRule = null, createTime = null, filterType = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbTraceFilterRule.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbTraceFilterRule.cl_traceFilterRule)) {
                        traceFilterRule = columnsValue;
                    } else if (columnsName.equals(DbTraceFilterRule.cl_createTime)) {
                        createTime = columnsValue;
                    } else if (columnsName.equals(DbTraceFilterRule.cl_filterType)) {
                        filterType = columnsValue;
                    }
                    DbTraceFilterRule pLogTraceFilterRule = new DbTraceFilterRule(rowKey, name, traceFilterRule, filterType, createTime);
                    result.add(pLogTraceFilterRule);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(TraceFilterRuleHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public ArrayList<String> getAllTraceFilterName() {
           PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_traceFilterRule"));
        List<KeySlice> crs = sm.getAll();
        ArrayList<String> traceFilterRuleNameList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String name = "";
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbTraceFilterRule.cl_name)) {
                        name = columnsValue;
                    }
                }
                if (!traceFilterRuleNameList.contains(name) && name.length() > 0) {
                    traceFilterRuleNameList.add(name);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(TraceFilterRuleHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return traceFilterRuleNameList;
    }

    public void deleteTraceFilterRule(final String rowKey) {
            PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_traceFilterRule"));
        sm.delete(rowKey);
//        sm.close();
    }
}
